﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace DCTLib.Common
{
    public class ValidationHelper
    {
        public static bool IsEmpty(HtmlInputText txtInput)
        {
            if (txtInput == null)
                return true;
            return txtInput.Value.Trim() == "";
        }

        public static bool IsEmpty(string input)
        {
            if (input == null)
                return true;
            return input.Trim() == "";
        }

        public static bool IsEmpty(TextBox txtInput)
        {
            if (txtInput == null)
                return true;
            return txtInput.Text.Trim() == "";
        }

        public static bool IsContainSpace(string input)
        {
            return input.Contains(" ");
        }

        public static bool IsNumber(string input)
        {
            try
            {
                int.Parse(input);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool IsValidEmailFormat(string inputEmail)
        {
            if ((inputEmail == null) || (inputEmail == string.Empty))
            {
                return false;
            }

            string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
                @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
                @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            Regex re = new Regex(strRegex);

            return re.IsMatch(inputEmail);
        }

        public static bool IsValidURL(string strURL)
        {
            string regExPattern = @"^^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_=]*)?$";
            Regex re = new Regex(regExPattern);
            return re.IsMatch(strURL);
        }

        public static bool HasRow(DataTable input)
        {
            return ((input != null) && (input.Rows.Count > 0));
        }
    }
}
